package com.lse_api.config.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lse_api.config.security.AuthTokenConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.List;

/**
 * LSE 请求拦截器
 *
 * @author 祁雪
 */
@EnableWebMvc
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {

    @Value("${web.upload-path}")
    public String path;

    /**
     * 注入跨域拦截器
     *
     * @return
     */
    @Bean
    public CrossInterceptorConfig getCrossInterceptorConfig() {
        return new CrossInterceptorConfig();
    }

    /**
     * 注入用户验证
     *
     * @return
     */
    @Bean
    public UserInterceptorConfig getUserInterceptorConfig() {
        return new UserInterceptorConfig();
    }

    /**
     * 注入spring security token鉴权控制
     *
     * @return
     */
    @Bean
    public AuthTokenConfig getAuthTokenConfig() {
        return new AuthTokenConfig();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 添加跨域过滤器
        registry.addInterceptor(getCrossInterceptorConfig()).addPathPatterns("/**");
        // 添加API用户认证过滤
        registry.addInterceptor(getUserInterceptorConfig()).addPathPatterns("/lseapi/api/**");
        // 添加token鉴权拦截器(用户后台)
        registry.addInterceptor(getAuthTokenConfig()).addPathPatterns("/lseapi/backapi/**");
    }

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
        converters.add(messageConverter());
    }

    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    /**
     * 配置静态资源访问
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("file:" + path);
    }


    /**
     * 配置json返回转换器
     *
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter messageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(getObjectMapper());
        return converter;
    }

    @Bean
    public ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

}
